/*
定义
1:导入  defineStore
2:定义defineStore调用
   export const useUserStore=defineStore(
      'user',
      ()=>{
        return {}
      },
      {

      }
   )

3:根模块统一暴露
   index.ts
    export * from './user'

使用：
  1：导入
     import {名字} from '@/stores/index'
  2:
    const userStore=名字()
    const {testNum}=storeToRefs(userStore)


持久化
  pinia-plugin-persistedstate
  1:下载
  2：导入配制
     stores/index.ts中导入配制
     import persistedstate from 'pinia-plugin-persistedstate'
     const pinia=createPinia()
     pinia.use(persistedstate)
  3:到相应模块内使用
    defineStore('','',{
         persist:true  全部持久化
         persist:{
            paths:['字段名']
         } 
    })

*/
import type { loginPasswordApi } from '@/types/test'
import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useUserStore = defineStore(
  'user',
  () => {
    const testNum = ref(999)
    const setTestNum = () => {
      testNum.value++
    }
    // 存储用户信息与token
    const userInfo = ref<loginPasswordApi>()
    const setUserInfo = (val: loginPasswordApi) => {
      userInfo.value = val
    }
    // 删除token与用户信息
    const logout = () => {
      userInfo.value = undefined
    }

    // 存储回跳地址
    const backUrl = ref('/')
    const setBackUrl = (url: string) => {
      backUrl.value = url
    }

    return { testNum, setTestNum, userInfo, setUserInfo, logout, backUrl, setBackUrl }
  },
  {
    persist: {
      paths: ['testNum', 'userInfo', 'backUrl']
    }
  }
)
